package simulator.meta.environment;

import java.awt.Color;
import java.util.LinkedList;

import javax.vecmath.Color3f;

import simbad.sim.Arch;
import simbad.sim.EnvironmentDescription;
import simbad.sim.SimpleAgent;
import simbad.sim.Wall;
import simulator.config.Config;
import simulator.implementation.agent.essential.ControlledAgentWithInteralRepresentation;
import simulator.meta.agent.ControlledAgent;
import simulator.meta.environment.bean.EnviromentBean;
import simulator.meta.environment.gui.KeyController;

public class EnvironmentSimulator extends EnvironmentDescription{

	private LinkedList<SimpleAgent> agents;

	public EnvironmentSimulator() {
		this.agents = new LinkedList<SimpleAgent>();
		this.setFloorStyle(TILE_FLOOR);
		this.floorColor = new Color3f(Color.BLUE);
		
		
		EnviromentBean enviromentBean = EnvironmentBuilderXML.parse(Config.SIMULATION_DESCRIPTOR, this);
		//Enviroment:
		float size = enviromentBean.getEnvironmentSize();
		if(size>0){
			this.setWorldSize(size);
		}
		int numberOfTileSide = enviromentBean.getNumberOfSideTiles();
		this.setNumberSideTiles(numberOfTileSide);
		// Stuff:
		LinkedList<Wall> walls = enviromentBean.getWallList();
		for (Wall wall : walls) {
			this.add(wall);
		}
		LinkedList<Arch> archs = enviromentBean.getArchList();
		for (Arch arch : archs) {
			this.add(arch);
		}
		//Agents:
		LinkedList<SimpleAgent> agents = enviromentBean.getAgentList();
		for (SimpleAgent agent : agents) {
			this.add(agent);
			this.agents.add(agent);
			if(agent instanceof ControlledAgent){
				new KeyController((ControlledAgent)agent);
			}
			if(agent instanceof ControlledAgentWithInteralRepresentation){
				((ControlledAgentWithInteralRepresentation)agent).setAwarenessAboutDisplacementWithinTheWorld((int) super.worldSize, super.numberSideTiles);
			}
		}
	}
	
}
